import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useNovelStore = defineStore('novel', () => {
  // 状态
  const loading = ref(false)
  const novels = ref([])
  const categories = ref([])
  const pagination = ref({
    total: 0,
    page: 1,
    page_size: 20,
    total_pages: 1
  })
  
  // 筛选条件
  const filters = ref({
    page: 1,
    page_size: 20,
    category_id: '',
    keyword: '',
    status: '',
    is_featured: false
  })
  
  // Actions
  const setLoading = (value) => {
    loading.value = value
  }
  
  const setNovels = (data) => {
    novels.value = data
  }
  
  const setPagination = (data) => {
    pagination.value = data
  }
  
  const setCategories = (data) => {
    categories.value = data
  }
  
  const updateFilter = (key, value) => {
    filters.value[key] = value
    if (key !== 'page') {
      filters.value.page = 1 // 重置到第一页
    }
  }
  
  const resetFilters = () => {
    filters.value = {
      page: 1,
      page_size: 20,
      category_id: '',
      keyword: '',
      status: '',
      is_featured: false
    }
  }
  
  return {
    loading,
    novels,
    categories,
    pagination,
    filters,
    setLoading,
    setNovels,
    setPagination,
    setCategories,
    updateFilter,
    resetFilters
  }
})

